﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Code;
using Sausage.Web.DataModel;
using Sausage.Web.Domain;
using Sausage.Web.Models;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 项目日志控制器
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2012/02/22 12:26    创建
    /// </history>
    public class ProjectJournalController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 审核。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Audit()
        {
            return View();
        }

        /// <summary>
        /// 签审。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Sign()
        {
            return View();
        }

        /// <summary>
        /// 确认。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Confirm()
        {
            return View();
        }

        /// <summary>
        /// 查看工程日志。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewJournal(Guid journalID)
        {
            //返回视图
            return View(ProjectJournalHelper.GetProjectJournalByID(journalID));
        }

        /// <summary>
        /// 查看工程日志。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewProjectJournal(Guid projectID)
        {
            return View(ProjectHelper.GetProjectByID(projectID));
        }

        /// <summary>
        /// 查看工程日志。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewQuantityJournal(Guid quantityID)
        {
            //返回视图
            return View(ProjectQuantityHelper.GetProjectQuantityByID(quantityID));
        }

        /// <summary>
        /// 查询工程日志数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程日志信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {
            var result = ProjectJournalHelper.SubmitSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程日志加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程日志数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程日志信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SignSelect(JosnPagingQueryModel query)
        {
            var result = ProjectJournalHelper.SignSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程日志加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程日志数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程日志信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ConfirmSelect(JosnPagingQueryModel query)
        {
            var result = ProjectJournalHelper.ConfirmSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程日志加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程日志数据。
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程日志信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult AuditSelect(JosnPagingQueryModel query)
        {
            var result = ProjectJournalHelper.AuditSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程日志加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = ProjectJournalHelper.Select(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程日志加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid journalID)
        {
            var item = ProjectJournalHelper.GetProjectJournalByID(journalID);

            if (item == null)
                return new JsonNetResult()
                {
                    Data = new JsonResultViewModel
                    {
                        success = false,
                        message = "指定的项目工程日志不存在！"
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };

            var data = new
            {
                item.JournalID,
                item.OrganizationID,
                Organization = item.Organization.OrganizationName,
                item.ProjectID,
                ProjectCode = item.Project.ProjectCode,
                ProjectName = item.Project.ProjectName,
                item.Weather,
                item.Content,
                item.CreateDate,
                item.Applicant,
                item.ApplicationDate,
                item.Auditor,
                item.AuditedDate,
                item.Signer,
                item.SignedDate,
                item.Approver,
                item.ApprovedDate,
                item.State,
                item.IsDelay,
                item.Memo
            };

            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = data,
                    total = 1,
                    message = "项目工程日志加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(ProjectJournalListRow data)
        {
            ProjectJournalHelper.DeleteProjectJournal(data.JournalID);
            return Json(new JsonResultViewModel { success = true, message = "项目日志删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [ValidateInput(false)]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(ProjectJournalListRow data)
        {
            ProjectJournalHelper.UpdateProjectJournal(data);
            return Json(new JsonResultViewModel { success = true, message = "项目日志更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <returns>创建结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [ValidateInput(false)]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(ProjectJournalListRow data)
        {
            string errorMessage;
            if (!Helper.CouldSet(data.ApplicationDate, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage, data = Guid.Empty }, JsonRequestBehavior.AllowGet);

            Guid journalID = ProjectJournalHelper.CreateProjectJournal(data);
            return Json(new JsonResultViewModel { success = true, message = "项目日志创建成功！", data = journalID }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查询图片信息
        /// </summary>
        /// <returns>查询结果。</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Photos(Guid journalID)
        {
            var data = ProjectJournalHelper.GetJournalPhotos(journalID);
            return Json(new JsonResultViewModel { success = true, message = "项目日志照片加载成功！", data = data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public ActionResult Photo(Guid photoID)
        {
            var photo = ProjectJournalHelper.GetPhoto(photoID);

            if (photo == null)
                return Json(new JsonResultViewModel { success = false, message = "没有此项目日志照片！", data = Guid.NewGuid() }, JsonRequestBehavior.AllowGet);

            return
                new ImageResult(
                    string.Concat(Server.MapPath(Utility.ProjectJournalFolder), Path.DirectorySeparatorChar,
                                  photo.PhotoID, photo.FileExtension),
                    Utility.GetContentTypeFromFileExtension(photo.FileExtension));
        }

        /// <summary>
        /// 删除照片
        /// </summary>
        /// <returns>删除结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult DeletePhoto(Guid id)
        {
            if (ProjectJournalHelper.DeleteProjectJournalPhoto(id))
                return Json(new JsonResultViewModel { success = true, message = "项目日志图片删除成功！", data = Guid.NewGuid() },
                            JsonRequestBehavior.AllowGet);
            return Json(new JsonResultViewModel { success = false, message = "项目日志图片删除失败！", data = Guid.NewGuid() },
                        JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSubmit(Guid[] journals)
        {
            ProjectJournalHelper.ExecuteSubmit(journals);
            return Json(new JsonResultViewModel { success = true, message = "项目日志提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="labours">The labours.</param>
        /// <param name="name">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteAudit(Guid[] journals, bool passed, string comment)
        {
            ProjectJournalHelper.ExecuteAudit(journals, passed, comment);
            return Json(new JsonResultViewModel { success = true, message = "项目日志审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 批准
        /// </summary>
        /// <param name="labours">The labours.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteConfirm(Guid[] journals, bool passed, string comment)
        {
            ProjectJournalHelper.ExecuteConfirm(journals, passed, comment);
            return Json(new JsonResultViewModel { success = true, message = "项目日志审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 签审
        /// </summary>
        /// <param name="labours">The labours.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSign(Guid[] journals, bool passed)
        {
            ProjectJournalHelper.ExecuteSign(journals, passed);
            return Json(new JsonResultViewModel { success = true, message = "项目日志签审成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
